package br.com.sgh.suport;

import br.com.sgh.dao.EmprestimoDAO;
import br.com.sgh.entidade.Emprestimo;
import br.com.sgh.util.FacesContextUtil;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

@ManagedBean
@RequestScoped
public class DevolucaoPendente implements Serializable {

    private StreamedContent arquivoRetorno;
    private Emprestimo emprestimo;
    private List<Emprestimo> emprestimos;

    private EmprestimoDAO emprestimoDAO() {
        EmprestimoDAO<Emprestimo> dao = new EmprestimoDAO<Emprestimo>(Emprestimo.class, FacesContextUtil.getRequestSession());
        return dao;
    }

    public StreamedContent getArquivoRetorno() throws DocumentException, FileNotFoundException, IOException {
        System.err.println("Entro no metodo");
        Document document = new Document(PageSize.A4);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        document.open();

        Font f = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
        Paragraph t1 = new Paragraph("SGBC - BIBLIOTECA CORPORATIVA", f);
        t1.setAlignment(Element.ALIGN_CENTER);
        document.add(t1);

        Paragraph t2 = new Paragraph("SORRISO - MT", f);
        t2.setAlignment(Element.ALIGN_CENTER);
        document.add(t2);

        Paragraph p = new Paragraph("Lista de Devoluções Pendentes");
        p.setAlignment(Element.ALIGN_CENTER);
        document.add(p);
        try {
            emprestimos = emprestimoDAO().listarImprestimoPendentes();

            Font fc = new Font(Font.FontFamily.TIMES_ROMAN, 8);
            Font fh = new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.BOLD);

            SimpleDateFormat sfd = new SimpleDateFormat("dd/MM/yyyy");

            if (emprestimos.size() > 0) {

                PdfPTable table = new PdfPTable(new float[]{0.07f, 0.27f, 0.14f, 0.14f, 0.38f});

                table.setSpacingBefore(5.0f);
                table.setWidthPercentage(100.0f);

                table.setHorizontalAlignment(Element.ALIGN_LEFT);

                table.addCell(new Phrase("Código", fh));
                table.addCell(new Phrase("Título", fh));
                table.addCell(new Phrase("Emprestimo", fh));
                table.addCell(new Phrase("Devolução", fh));
                table.addCell(new Phrase("Para", fh));

                for (Emprestimo e : emprestimos) {
                    table.addCell(new Phrase(String.valueOf(e.getAcervo().getCodAcervo()), fc));
                    table.addCell(new Phrase(e.getAcervo().getTitulo().getDescricao(), fc));
                    table.addCell(new Phrase(String.valueOf(sfd.format(e.getDataEmprestimo())), fc));
                    table.addCell(new Phrase(String.valueOf(sfd.format(e.getPrevisaoEntrega())), fc));
                    table.addCell(new Phrase(e.getFuncionario().getNome(), fc));
                }
                document.add(table);

            }
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao tentar Gerar o Relatório", "Erro"));
        }
        //document.add(table);
        document.close();
        FileOutputStream fos = new FileOutputStream("C:\\PDF\\arqPDFexemplo2.pdf");
        InputStream arquivo = new FileInputStream("C:\\PDF\\arqPDFexemplo2.pdf");
        fos.write(baos.toByteArray());
        fos.close();

        arquivoRetorno = new DefaultStreamedContent(arquivo, "application/pdf", "reltorio.pdf");

        return arquivoRetorno;
    }

    public void setArquivoRetorno(StreamedContent arquivoRetorno) {
        this.arquivoRetorno = arquivoRetorno;
    }

    public Emprestimo getEmprestimo() {
        return emprestimo;
    }

    public void setEmprestimo(Emprestimo emprestimo) {
        this.emprestimo = emprestimo;
    }

    public List<Emprestimo> getEmprestimos() {
        return emprestimos;
    }

    public void setEmprestimos(List<Emprestimo> emprestimos) {
        this.emprestimos = emprestimos;
    }

}
